const userService = require("../service/user.service");
const { PARAMETER_MISSING } = require("../config/error-constants");
const { getMissingKeys, paging_Parameter } = require("../config/parameter");

class UserController {
  async create(ctx, next) {
    //3.将user信息存储到数据库中
    //   create函数是一个异步函数，
    //   需要等待他的结果，再看看异步函数执行是成功还是失败
    const userInfo = ctx.request.body;
    const result = await userService.create(userInfo);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: "创建用户成功~",
      data: result,
    };
  }

  // 更新用户名字
  async updateName(ctx, next) {
    const id = ctx.userInfo.id;
    const { newName } = ctx.request.body;

    if (!newName) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "newName");
    }

    const result = await userService.updateNameService(id, newName);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `更新用户昵称为${newName}成功~`,
      data: result,
    };
  }

  // 更新用户头像
  async updateAvatar(ctx, next) {
    const id = ctx.userInfo.id;
    const { newAvatar } = ctx.request.body;

    if (!newAvatar) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "newAvatar");
    }

    const result = await userService.updateAvatarService(id, newAvatar);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `更新用户头像为${newAvatar}成功~`,
      data: result,
    };
  }

  // 获取用户信息
  async getUserInfo(ctx, next) {
    const id = ctx.userInfo.id;

    const result = await userService.inquireUser(id);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取用户信息成功~`,
      data: {
        userInfo: result[0],
      },
    };
  }

  // 获取用户的关注
  async getUserAttention(ctx, next) {
    const id = ctx.userInfo.id;

    /* 验证参数 */
    /* 如果不浅拷贝出来，直接写ctx.query，值为[Object: null prototype] { limit: '3' } */
    const pagingParameter = getMissingKeys(paging_Parameter, { ...ctx.query });
    // console.log("pagingParameter", pagingParameter);
    if (pagingParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, pagingParameter);
    }

    const { limit, offset } = ctx.query;
    const result = await userService.getUserAttentionService(id, limit, offset);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取用户关注成功~`,
      data: result,
    };
  }

  // 获取用户的收藏
  async getUserSave(ctx, next) {
    const id = ctx.userInfo.id;

    /* 验证参数 */
    /* 如果不浅拷贝出来，直接写ctx.query，值为[Object: null prototype] { limit: '3' } */
    const pagingParameter = getMissingKeys(paging_Parameter, { ...ctx.query });
    // console.log("pagingParameter", pagingParameter);
    if (pagingParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, pagingParameter);
    }

    const { limit, offset } = ctx.query;
    const result = await userService.getUserSaveService(id, limit, offset);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取用户收藏成功~`,
      data: result,
    };
  }

  // 获取用户的点赞
  async getUserSupport(ctx, next) {
    const id = ctx.userInfo.id;

    /* 验证参数 */
    /* 如果不浅拷贝出来，直接写ctx.query，值为[Object: null prototype] { limit: '3' } */
    const pagingParameter = getMissingKeys(paging_Parameter, { ...ctx.query });
    // console.log("pagingParameter", pagingParameter);
    if (pagingParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, pagingParameter);
    }

    const { limit, offset } = ctx.query;
    const result = await userService.getUserSupportService(id, limit, offset);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取用户点赞成功~`,
      data: result,
    };
  }

  // 获取用户的发布
  async getUserPublish(ctx, next) {
    const id = ctx.userInfo.id;

    /* 验证参数 */
    /* 如果不浅拷贝出来，直接写ctx.query，值为[Object: null prototype] { limit: '3' } */
    const pagingParameter = getMissingKeys(paging_Parameter, { ...ctx.query });
    // console.log("pagingParameter", pagingParameter);
    if (pagingParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, pagingParameter);
    }

    const { limit, offset } = ctx.query;
    const result = await userService.getUserPublishService(id, limit, offset);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取用户的发布成功~`,
      data: result,
    };
  }

  // 关注用户
  async attentionUser(ctx, next) {
    const uid = ctx.userInfo.id;

    const { attention_uid } = ctx.params;

    /* 验证参数 */
    if (!attention_uid) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "attention_uid");
    }

    const result = await userService.attentionUserService(uid, attention_uid);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `关注用户成功~`,
      data: result,
    };
  }

  // 取消关注用户
  async cancelAttentionUser(ctx, next) {
    const uid = ctx.userInfo.id;

    const { attention_uid } = ctx.params;

    /* 验证参数 */
    if (!attention_uid) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "attention_uid");
    }

    const result = await userService.cancelAttentionUserService(
      uid,
      attention_uid
    );

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `取消关注用户成功~`,
      data: result,
    };
  }

  // 获取是否关注用户
  async isAttentionUser(ctx, next) {
    const uid = ctx.userInfo.id;

    const { attention_uid } = ctx.params;

    /* 验证参数 */
    if (!attention_uid) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "attention_uid");
    }

    const result = await userService.isAttentionUserService(uid, attention_uid);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取是否关注用户成功~`,
      data: result,
    };
  }

  // 获取是否收藏文章
  async isSaveArticle(ctx, next) {
    const uid = ctx.userInfo.id;

    const { article_id } = ctx.params;

    /* 验证参数 */
    if (!article_id) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "article_id");
    }

    const result = await userService.isSaveArticleService(uid, article_id);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取是否收藏文章成功~`,
      data: result,
    };
  }

  // 获取是否点赞文章
  async isSupportArticle(ctx, next) {
    const uid = ctx.userInfo.id;

    const { article_id } = ctx.params;

    /* 验证参数 */
    if (!article_id) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "article_id");
    }

    const result = await userService.isSupportArticleService(uid, article_id);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取是否点赞文章成功~`,
      data: result,
    };
  }

  // 推荐用户
  async recommendUsers(ctx, next) {
    const { uid, limit } = ctx.query;

    if (!limit) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "limit");
    }

    const result = await userService.recommendUsersService(uid, limit);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取推荐用户成功~`,
      data: result,
    };
  }

  // 获取用户热线
  async getUserRescueHotline(ctx, next) {
    const { province } = ctx.query;

    if (!province) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "province");
    }

    const result = await userService.getUserRescueHotlineService(province);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取推荐用户所在地区救助热线成功~`,
      data: result,
    };
  }
}

module.exports = new UserController();
